package com.example.shoppingmallclient.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.example.shoppingmallclient.R;
import com.example.shoppingmallclient.adapter.CartListAdapter;
import com.example.shoppingmallclient.vo.CartItemVo;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("all")
public class MainActivity extends AppCompatActivity {

    private List<CartItemVo> list = new ArrayList<>();
    private String cartTotalPrice;
    private TextView tvCartTotalPrice;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart_list);

        tvCartTotalPrice = findViewById(R.id.tv_cart_total_price);
        listView = findViewById(R.id.lv);
        new getCartList().execute();
    }

    private class getCartList extends AsyncTask{

        @Override
        protected Object doInBackground(Object[] objects) {
            try {
                URL url = new URL("http://192.168.3.144:8080/CartShowServlet");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");

                InputStream is = connection.getInputStream();
                StringBuffer stringBuffer = new StringBuffer();
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1){
                    stringBuffer.append(new String(bytes, 0, len));
                }

                //System.out.println(stringBuffer.toString());
                String substring = stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
                String[] strings = substring.split("#");

                cartTotalPrice = strings[0];
                System.out.println(cartTotalPrice);
                for(int i = 1; i < strings.length; i++){
                    String[] split = strings[i].split("@");

                    String image = split[0];
                    URL imgUrl = new URL(image);
                    InputStream inputStream = imgUrl.openStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                    CartItemVo cartItemVo = new CartItemVo();
                    cartItemVo.setImage(bitmap);
                    cartItemVo.setBaseInformation(split[1]);
                    cartItemVo.setPrice(Double.parseDouble(split[2]));
                    cartItemVo.setQuantity(Integer.valueOf(split[3]));
                    cartItemVo.setSumPrice(Double.parseDouble(split[4]));
                    list.add(cartItemVo);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            tvCartTotalPrice.setText("总价：" + cartTotalPrice);
            CartListAdapter cartListAdapter = new CartListAdapter(list, R.layout.cart_item, MainActivity.this);
            listView.setAdapter(cartListAdapter);
        }
    }

}